using System;
using System.Globalization;
using System.Text;
using System.Windows.Data;

namespace KnowledgeKiosk.Converters
{
    /// <summary>
    /// Provides a way to convert a DateTime value to a collection of glyph specifications.
    /// </summary>
    /// <remarks>
    /// This is a very specific converter required by the DigitalClockControl to keep the individual characters 
    /// of the clock's font aligned. The character '1' has a different width and offset than all the other
    /// characters, thus causing alignment issues when the time contains one or more '1' characters. 
    /// Alignment is required to make the foreground-background overlay work.
    /// </remarks>
    public class TimeToIndicesConverter : IValueConverter
    {
        /// <summary>
        /// Glyph specification with fixed width and offset
        /// </summary>
        private const string GLYPH_ADVANCEMENT = ",51,27;";

        /// <summary>
        /// Empty glyph specification
        /// </summary>
        private const string SKIP_ADVANCEMENT = ";";

        #region IValueConverter Members

        /// <summary>
        /// Converts a DateTime value to a collection of glyph specifications
        /// </summary>
        /// <param name="value">The DateTime value that will be converted</param>
        /// <param name="targetType">This parameter is ignored.</param>
        /// <param name="parameter">This parameter is ignored.</param>
        /// <param name="culture">This parameter is ignored.</param>
        /// <returns>A collection of glyph specifications.</returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var dateTime = (DateTime) value;
            var sb = new StringBuilder();
            foreach (char c in dateTime.ToString("HH:mm"))
                if (c == '1')
                    sb.Append(GLYPH_ADVANCEMENT);
                else
                    sb.Append(SKIP_ADVANCEMENT);
            return sb.ToString();
        }

        /// <summary>
        /// Converts a collection of glyph specifications to a time, which is impossible, conceptually meaningless and
        /// not required for the DigitalClockControl to function.
        /// </summary>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}